/*********************************************************************
Replication Materials for "Explanation Giving Promotes Democratic Satisfaction Regardless of Respondent Partisanship"

Syntax produces the models reported in Table 1 and 2 in-text and their accompanying full-model versions in the Online Appendices. 

Syntax also saves the output that will be plotted in Figures 4/5/6 in-text (via an R syntax file)
***********************************************************************/

/*******************
Working Directory and Data
Update working directory to where you have the data
********************/

cd "C:\Users\jar68\OneDrive\Ongoing Work\Explanations and Dem Support\P. Behavior\Revision\R2\RR3\FINAL\Dataverse\"
use "ess_final.dta"

**Dropping non-democracies; Bulgaria had caretaker gov't
drop if country == "Albania" | country == "Russia" | country == "Kosovo" | country == "Ukraine" | country == "Bulgaria"

/*********************************
Weights
*********************************/

gen anweight = pspwght*pweight
label var anweight "Analysis Weight"



/*********************************
Main Models (Table 1)
*********************************/

***Intra Class Correlation (ICC)
mixed gov_satis [pweight=anweight] || country: 
estat icc
mixed dem_satis [pweight=anweight] || country: 
estat icc

/*
ICC Statistics 
	Gov Satis: 0.195
	Dem Satis: 0.188
*/

/********************
Models for manuscript: 
	Model 1: Just Explanation Giving
	Model 2: Add Controls
	Model 3: Interactions
*********************/

********Models********

eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' std_explains reason_std [pweight=anweight] || country:  
			margins, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_m1, replace)
			margins, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_m1, replace)
	eststo: mixed `var' std_explains reason_std i.co_partisan `conts'  [pweight=anweight] || country: 
			margins, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_m2, replace)
			margins, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_m2, replace)
	eststo: mixed `var' c.std_explains##i.co_partisan c.reason_std##i.co_partisan `conts' [pweight=anweight] || country: 
			*explains * co-partisanship
				margins, dydx(std_explains) by(co_partisan) saving(`var'_explain_dydx_m3, replace)
				margins, dydx(co_partisan) at(std_explains=(-2(1)2)) saving(`var'_cop_explain_dydx_m3, replace)
				margins co_partisan, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_m3, replace)
			*reasons * co-partisanship
				margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_m3, replace)
				margins, dydx(co_partisan) at(reason_std=(-2(1)2)) saving(`var'_cop_reason_dydx_m3, replace)
				margins co_partisan, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_m3, replace)
}

********Tables*********
***full results to be reported in appendix
***Table 1 created by manually removing non-needed elements and some other aesthetic alterations)

esttab using table_oa2.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Dem" "Dem" "Dem") ///
		title("{\b Table OA2}: Explanation Giving and Political Support") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights.")


********Effect Sizes******
summ dem_satis gov_satis	

/*
The following re-runs the models and calculates the difference between those
at the 90th and 10th percentiles (or out vs. co-partisans in the fianl instance). 
Estimates are saved to file for later plotting in R.
*/


***Dem Satisfaction
foreach var in std_explains reason_std std_courts std_elections std_econ std_socialtrust std_ladder unemp_ch {
	mixed dem_satis std_explains reason_std i.co_partisan std_courts std_elections std_econ std_life std_socialtrust ///
		i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch [pweight=anweight] || country:  
		margins, at( (p90) `var') at( (p10) `var') post 
		lincomest _b[1bn._at] -  _b[2._at]
		parmest, label saving(lincom_dem_`var', replace)
}
	
*Partisanship
mixed dem_satis std_explains reason_std i.co_partisan std_courts std_elections std_econ std_life std_socialtrust ///
		i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch [pweight=anweight] || country: 
margins, dydx(2.co_partisan) saving(lincom_dem_opposing, replace)

***Gov Satisfaction
foreach var in std_explains reason_std std_courts std_elections std_econ std_socialtrust std_ladder unemp_ch {
	mixed gov_satis std_explains reason_std i.co_partisan std_courts std_elections std_econ std_life std_socialtrust ///
		i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch [pweight=anweight] || country:  
		margins, at( (p90) `var') at( (p10) `var') post 
		lincomest _b[1bn._at] -  _b[2._at]
		parmest, label saving(lincom_gov_`var', replace)
}
	
*Partisanship
mixed gov_satis std_explains reason_std i.co_partisan std_courts std_elections std_econ std_life std_socialtrust ///
		i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch [pweight=anweight] || country: 
margins, dydx(2.co_partisan) saving(lincom_gov_opposing, replace)

/*********************************
Clustered Standard Error Models (Table 2)
*********************************/

****No Interaction (Appendix Table)
eststo clear
local indiv_conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen"
local country_conts " i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' reason_std [pweight = anweight] || country:, vce(cluster country)
	eststo: mixed `var' reason_std std_explains [pweight = anweight] || country:, vce(cluster country)
	eststo: mixed `var' reason_std `country_conts' [pweight = anweight] || country:, vce(cluster country)
	eststo: mixed `var' reason_std  std_explains i.co_partisan `indiv_conts' [pweight = anweight] || country:, vce(cluster country)
	eststo: mixed `var' reason_std  std_explains i.co_partisan `indiv_conts' `country_conts'  [pweight = anweight] || country:, vce(cluster country)
}

*Table
esttab using table_oa7.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Gov" "Gov" "Dem" "Dem" "Dem" "Dem" "Dem") ///
		title("{\b Table OA7}: Explanation Giving and Political Support") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights and standard errors clustered at the country level.")


*****Interaction Models (Appendix Table)
eststo clear
local indiv_conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen"
local country_conts " i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' c.reason_std##i.co_partisan [pweight=anweight] || country: , vce(cluster country)
	eststo: mixed `var' c.reason_std##i.co_partisan `country_conts' [pweight=anweight] || country: , vce(cluster country)
	eststo: mixed `var' c.reason_std##i.co_partisan c.std_explains##i.co_partisan `indiv_conts' [pweight=anweight] || country: , vce(cluster country)
	eststo: mixed `var' c.reason_std##i.co_partisan c.std_explains##i.co_partisan `indiv_conts' `country_conts' [pweight=anweight] || country: , vce(cluster country)
}

*Table 
esttab using table_oa8.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Gov" "Dem" "Dem" "Dem" "Dem") ///
		title("{\b Table OA8}: Explanation Giving and Political Support") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights and standard errors clustered at the country level.")
	
*Margins
eststo clear
local indiv_conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen"
local country_conts " i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' c.reason_std##i.co_partisan [pweight=anweight] || country: , vce(cluster country)
		margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_m3_cluster1, replace)	
	eststo: mixed `var' c.reason_std##i.co_partisan `country_conts' [pweight=anweight] || country: , vce(cluster country)
		margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_m3_cluster2, replace)		
	eststo: mixed `var' c.reason_std##i.co_partisan c.std_explains##i.co_partisan `indiv_conts' [pweight=anweight] || country: , vce(cluster country)
		margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_m3_cluster3, replace)	
		margins, dydx(std_explains) by(co_partisan) saving(`var'_explain_dydx_m3_cluster3, replace)		
	eststo: mixed `var' c.reason_std##i.co_partisan c.std_explains##i.co_partisan `indiv_conts' `country_conts' [pweight=anweight] || country: , vce(cluster country)
		margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_m3_cluster4, replace)	
		margins, dydx(std_explains) by(co_partisan) saving(`var'_explain_dydx_m3_cluster4, replace)	
}


****In-Text Table

eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' std_explains reason_std [pweight=anweight] || country: , vce(cluster country)
	eststo: mixed `var' std_explains reason_std i.co_partisan `conts'  [pweight=anweight] || country:  , vce(cluster country)
	eststo: mixed `var' c.std_explains##i.co_partisan c.reason_std##i.co_partisan `conts' [pweight=anweight] || country:  , vce(cluster country)
}
esttab using table2.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Dem" "Dem" "Dem") ///
		title("{\b Table 2}: Explanation Giving and Political Support") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights and standard errors clustered at the country level.")
	